<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<style type="text/css">
			.chage_people{ padding: 0 15px;}
			.chage_people .form li:not(:first-child){ padding-bottom: 15px;}
			.chage_people .form b{ display:block; font-size: 18px; font-weight: 400; color: #333; line-height: 60px;}
			.chage_people .form label{ display:block; font-size: 14px; color: #808080; line-height:40px;}
			.chage_people .form input{ display:block; box-sizing: border-box; width: 100%; border: 1px #e0e0e0 solid; padding: 12px 0 12px 10px; border-radius: 4px; background-color: #fff;}
			.chage_people .form button{ display:block; border: 0; background: linear-gradient(to right, #3053F0, #469BF7); font-size: 14px; color: #fff; height: 45px; width:115px; padding: 0; margin-left: 15px;}
			.chage_people .btn{ padding-top: 25px;}
			.d_btn span{ opacity: 0.5; }
			.sk-three-bounce {
			  margin: 0 auto;
			  width: 80px;
			  text-align: center; }
			  .sk-three-bounce .sk-child {
				width: 13.5px;
				height: 13.5px;
				background-color: #3262F1;
				border-radius: 100%;
				display: inline-block;
				-webkit-animation: sk-three-bounce 1.4s ease-in-out 0s infinite both;
						animation: sk-three-bounce 1.4s ease-in-out 0s infinite both; }
			  .sk-three-bounce .sk-bounce1 {
				-webkit-animation-delay: -0.32s;
						animation-delay: -0.32s; }
			  .sk-three-bounce .sk-bounce2 {
				-webkit-animation-delay: -0.16s;
						animation-delay: -0.16s; }

			@-webkit-keyframes sk-three-bounce {
			  0%, 80%, 100% {
				-webkit-transform: scale(0);
						transform: scale(0); }
			  40% {
				-webkit-transform: scale(1);
						transform: scale(1); } }

			@keyframes sk-three-bounce {
			  0%, 80%, 100% {
				-webkit-transform: scale(0);
						transform: scale(0); }
			  40% {
				-webkit-transform: scale(1);
						transform: scale(1); }
			}
			
		</style>
	</head>
	<body>
		<div class="chage_people">
			<ul class="form">
				<li>
						<b>请绑定新受益人账号</b>
					</li>
					<li>
						<label>绑定新手机号码</label>
						<input type="number" name="mobile" id="mobile" value="" placeholder="请输入手机号码" />
					</li>
					<li>
						<label>短信验证码</label>
						<div class="m_cell_hd m_cell_primary"><input type="number" name="vcode" id="vcode" value="" placeholder="请输入短信验证码" /></div>
						<div class="m_cell_ft" id="senddiv"><button id="btnSendCode" tapmode onclick="sendMessage();">获取验证码</button></div>
					</li>
				</ul>
				<ul class="btn">
					<button class="d_btn" tapmode="btnpress" id="login-btn" onclick="changeperson();"><span>下一步</span></button>
				</ul>
			</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/app.js"></script>
	<script type="text/javascript" src="../../script/md5.js"></script>
	<script type="text/javascript" src="../../script/doT.js"></script>
	<script type="text/javascript">
		var insureid;
		var UIInput;
		var curPwdinputId = 0; //当前密码输入框ID
		var curPwdinputType = 'password'; //当前密码输入框类型

		var isrun = 0; //变量控制是否可以点击发短信按钮
		var InterValObj; //timer变量，控制时间
		var count = 60; //间隔函数，1秒执行
		var curCount;//当前剩余秒数
		var keyboardId = 0;//当前键盘ID
		apiready = function() {
			api.parseTapmode();
			insureid = api.pageParam.insureid;
			UIInput = api.require('UIInput');
			fnInitUIInput();
			api.addEventListener({
                name: 'navitembtn'
            }, function(ret, err) {
                if (ret.type == 'left') {
					api.closeWin({
					});
                }
            });
		}
		function sendMessage() {
			if(isrun == 1)
			{
				return false;
			}
			
			hideInput();//收起键盘
			//发送验证码
		  　curCount = count;
			var mobile = $api.val($api.byId('mobile'));
			if (!mobile) {
				toast('请输入手机号码');
				return false;
			}
			//if (mobile.search(/^([0-9]{11})?$/) == -1) {
			if(!(/^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(mobile))) {
				toast('手机号格式错误');
				return false;
			}
		　　//设置button效果，开始计时
			//$api.attr($api.byId('btnSendCode'),'disabled','true');
			$api.addCls($api.byId('btnSendCode'), 'disable-btn');
			$api.text($api.byId('btnSendCode'),"" + curCount + "s");
			InterValObj = JInterval(SetRemainTime,1000);
			isrun = 1;
			
			var paramsObj = {
				app_key: kAppKey,
				mobile: mobile,
				deviceId: api.deviceId,
				sendtype: 2
			}
			var _sign = getSign(paramsObj, kAppKey, kAppSecret);
			paramsObj.signature = _sign;
			api.ajax({
				url: ApiUrl + '/v1/token/sendsms',
				method: 'post',
				data: {
					values: paramsObj
				}
			}, function(ret, err) {
				if (ret) {
					toast('验证码已发送');
				} else {
					toast(err.body.message);
					console.log(JSON.stringify(err));
				}
			});
			
		}
		function JInterval(funcName,time){
			   var args=[];
			   for(var i=2;i<arguments.length;i++){
				   args.push(arguments[i]);
			   }
			return window.setInterval(function(){
			 funcName.apply(this,args);
			},time);
		}
		   
		//timer处理函数
		function SetRemainTime() {
			if (curCount == 0) {                
				isrun = 0;
				window.clearInterval(InterValObj);//停止计时器
				//$api.removeAttr($api.byId('btnSendCode'), 'disabled')
				$api.removeCls($api.byId('btnSendCode'), 'disable-btn');
				$api.text($api.byId('btnSendCode'),"重发验证码");
			}
			else {
				curCount--;
				$api.text($api.byId('btnSendCode'),"" + curCount + "s");
			}
		}
		function hideInput(){
			//隐藏键盘
			UIInput.closeKeyboard({
				id: keyboardId
			});
		}
		function fnInitUIInput(){
			//计算位置
			openUIinput('mobile','请输入手机号码','number','text','',11);
			openUIinput('vcode','请输入验证码','number','text','',8);
			
		}
		function openUIinput(domid,placeholder_str,keyboardType,inputType,pwdval,maxnum){
			//打开UIinput
			var password = $api.byId(domid);
			var password_rect = $api.offset(password);
			UIInput.open({
				rect: {
					x: password_rect.l+10,
					y: password_rect.t+3,
					w: password_rect.w-20,
					h: password_rect.h -6
				},
				styles: {
					bgColor: '#fff',
					size: 14,
					color: '#333',
					placeholder: {
						color: '#D4D4D4'
					}
				},
				autoFocus: false,
				maxRows: 1,
				placeholder: placeholder_str,
				keyboardType: keyboardType,
				inputType: inputType,
				fixedOn: api.frameName,
				fixed: false
			}, function(ret) {
				
				if (ret.eventType == 'change') {
					keyboardId = ret.id;
					UIInput.value({
							id: ret.id,
							//msg : '设置输入框的值'
					}, function(ret, err) {
						if (ret) {
							var _msg = ret.msg;
							if(maxnum > 0) {
								var _msglen = _msg.length;
								if(_msglen > maxnum)
								{
									_msglen = maxnum;
									_msg = ret.msg.substr(0,maxnum);
									UIInput.value({
										id: keyboardId,
										msg: _msg
									});
								}
							}
							$api.val(password,_msg);
						} else {
							//alert(JSON.stringify(err));
						}
					});
				} else if (ret.eventType == 'show' && domid == 'password') {
					curPwdinputId = ret.id; //获取验证码输入框ID
					if(pwdval!='')
					{
						UIInput.value({
							id: curPwdinputId,
							msg: pwdval
						});
					}
				} else {
					//alert(JSON.stringify(ret));
				}
			});
			
		}
		function changeperson() {
			
			hideInput();//收起键盘
			var vcode = $api.val($api.byId('vcode')).trim();
			var mobile = $api.val($api.byId('mobile')).trim();
			
			if($api.cssVal($api.byId('login-btn'),'opacity')!=1)
			{
				return false;
			}
			if(vcode=='')
			{
				toast("请输入验证码");
				return false;
			}
			showbtnloading('login-btn');
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret && ret.value)
				{
					var userinfo = JSON.parse(ret.value);
					if(userinfo.mobile==mobile)
					{
						toast("受益人不能变更为自己");
						return false;
					}
					console.log(kAppKey+';'+userinfo.access_token+';'+userinfo.uid);
					api.ajax({
						url: ApiUrl + '/v1/insuredetail',
						method: 'post',
						cache: false,
						timeout: 10,
						headers: {
							authorization: kAppKey+';'+userinfo.access_token+';'+userinfo.uid
						},
						data: {
							values: {
								type: 'changeperson',
								vcode: vcode,
								mobile: mobile,
								insureid: insureid
							}
						}
					}, function(ret, err) {
						if (ret) {
							api.sendEvent({
								//通知更新状态
								name : 'refreshinsure'
							});
							api.execScript({
								name: 'myinsuredetail_win',
								script: 'changetishi();'
							});
							openAlert();
							//var uid = ret.uid;
							//openNavWin('#ddd','changeshouyiren_win','changeshouyiren_frm.html','完善资料',false,false,true,[],{insureid:insureid,uid:uid});
						} else {
							console.log(JSON.stringify(err));
							toast(err.body.message);
						}
						hidebtnloading('login-btn','下一步');
					});
					}
			});
		}
		function openAlert()
		{
			var icon = 'sucess.png';
			var title = '联保资料上传成功';
			var msg = '请等待系统审核';
			var winname = 'myinsuredetail_win';
			var btntxt = '返回';
			var param = {
				icon: icon,
				title: title,
				msg: msg,
				btntxt: btntxt,
				winname: winname,
			};
			openNavWin('#ddd','noheadmsg_win','../public/noheadmsg_frm.html','变更受益人',false,true,false,[],param);
		}
	</script>
</html>